// pages/released/released.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: '',
    apiUrl: '',
    date: '',
    result: {
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var curDate = new Date();
    var myDate = new Date(curDate.getTime() + 24 * 60 * 60 * 1000); //后一天
    var time = myDate.toLocaleDateString().split('/').join('-');
    this.setData({
      staticUrl: app.globalData.staticUrl,
      apiUrl: app.globalData.apiUrl,
      date: "请选择时间"
    })
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate() + 1;
    if (month > 9) {
      this.setData({
        date: year + '-' + month + '-' + day
      })
    } else {
      this.setData({
        date: year + '-0' + month + '-' + day
      })
    }
    this.getRealse();
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    this.getRealse();
  },
  getRealse:function(){
    // 发起网络请求
    var userinfo = wx.getStorageSync("userInfo");
    wx.request({
      url: this.data.apiUrl + '/rele/getplan.do',
      data: {
        status: userinfo.status,
        session_key: wx.getStorageSync("session_key"),
        openid: wx.getStorageSync("openid"),
        cid: userinfo.cid,
        date: this.data.date
      },
      method: 'GET',
      success: (res) => {
        console.log(res, '已发布');
        var obj = {}
        var results = res.data.releaseNews;
        var morning = [];
        var afternoon = [];
        var night = [];
        results.forEach((item) => {
          if (item.time == 0) {
            morning.push(item);
          }
          if (item.time == 1) {
            afternoon.push(item);
          }
          if (item.time == 2) {
            night.push(item);
          }
        })
        obj.morning = morning;
        obj.afternoon = afternoon;
        obj.night = night;

        this.setData({
          result: obj
        })

        console.log(this.data.result)

      }
    })
  },
  gotoedit:function(){
    wx.navigateTo({
      url: '/pages/edit/edit?date=' + this.data.date,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})